package cn.kgc.util;


import cn.kgc.annotation.bean;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.*;

public class BeanUtils {
    private static String basePath;
    static{
        InputStream resourceAsStream = BeanUtils.class.getClassLoader().getResourceAsStream("bean.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            basePath = (String) properties.get("basePath");
            //System.out.println("basePath = " + basePath);
            basePath = basePath.replace(".", "/");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<String> getAllList() {
  ArrayList<String> fileNames= new ArrayList<String>();
        File file = new File("src/main/java/");
        String absolutePath = file.getAbsolutePath()+"/"+basePath;

        File file1 = new File(absolutePath);
        List<String> subFiles = findSubFiles(file1, fileNames);
        /*System.out.println("subFiles = " + subFiles);
        for (String subFile : subFiles) {
            System.out.println("subFile = " + subFile);
        }*/

        return fileNames;
    }
    public List<String> findSubFiles(File file,List<String> list){
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isDirectory()){
                findSubFiles(f,list);
            }else {


                if (f.length()==0){
                    f.delete();
                }
                String path = f.getPath();
                int start = path.indexOf("cn");
                int end = path.lastIndexOf(".java");
                String substring = path.substring(start, end).replace("\\",".");



                System.out.println("substring = " + substring);
                list.add(substring);
            }
        }

        return list;
    }
           public Map<String,Object> getAllBeans(List<String> fileNames){
               HashMap<String, Object> map = new HashMap<>();
               fileNames.forEach(file->{
                   try {
                       Class clazz = Thread.currentThread().getContextClassLoader().loadClass(file);
                       System.out.println("clazz = " + clazz);
                       if (!clazz.isAnnotation()&&!clazz.isInterface()) {
                           Annotation declaredAnnotation = clazz.getDeclaredAnnotation(bean.class);
                           if (declaredAnnotation != null) {
                               Constructor<?> declaredConstructor = clazz.getDeclaredConstructor();
                               declaredConstructor.setAccessible(true);
                               Object obj = declaredConstructor.newInstance();
                               int start = file.lastIndexOf(".");
                               String key = file.substring(start + 1).toLowerCase();
                               map.put(key, obj);
                           }
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               });
               return map;
           }
    public static void main(String[] args) {

        BeanUtils beanUtils = new BeanUtils();
        Map<String, Object> allBeans = beanUtils.getAllBeans(beanUtils.getAllList());
        System.out.println("allBeans = " + allBeans);
    }
}
